package com.augurit.common.system.service.org.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;

import com.augurit.common.dao.OmOrgAdminDao;
import com.augurit.common.system.convert.OmOrgAdminConverter;
import com.augurit.common.system.service.org.IOmOrgAdminService;
import com.augurit.common.system.web.org.form.OmOrgAdminForm;
import com.augurit.common.util.PageUtils;

/**
 * 机构管理员逻辑实现类
 */
@Service
@Transactional
public class OmOrgAdminServiceImpl implements IOmOrgAdminService {
	
	// DAO 引用
	@Autowired
	private OmOrgAdminDao omOrgAdminDao;
	
	
	/**
	 * 根据ID获取机构管理Form对象
	 * @param id 主键ID
	 * @return 机构管理Form对象
	 */
	@Transactional(readOnly = true)
	public OmOrgAdminForm get(Long id){
		return omOrgAdminDao.getForm(id);
	}

	/**
	 * 保存机构管理Form对象
	 * @param forms 机构管理Form对象
	 */
	public void save(OmOrgAdminForm... forms){
		if(forms != null)
			for(OmOrgAdminForm form : forms)
				omOrgAdminDao.save(form);
	}
	
	/**
	 * 根据主键ID删除机构管理Form对象
	 * @param ids 主键ID
	 */
	public void delete(Long... ids) {
		if(ids != null)
			omOrgAdminDao.delete(ids);
	}

	/**
	 * 分页查询
	 * @param page
	 * @param userId
	 * @return
	 */
	public Page<OmOrgAdminForm> searchAuthOrgs(Page<OmOrgAdminForm> page, Long userId){

		//建立分页对象
		Page pg = PageUtils.newPageInstance(page);
			
		String hql = "select ood, org.orgName from OmOrgAdmin ood, OmOrg org where org.orgId=ood.orgId and ood.userId=?";
		List values =  new ArrayList();
		values.add(userId);
		
		pg = omOrgAdminDao.findPage(pg, hql, values);
		List<OmOrgAdminForm> list = OmOrgAdminConverter.convertToForm(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
}
